package com.ldc.api.article;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ldc.api.common.ParamsHolder;
import com.ldc.common.utils.*;
import com.ldc.model.Article;
import com.ldc.model.base.ApiBean;
import com.ldc.service.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.util.List;

/**
 * 功能描述
 *
 * @author liudi
 * @since 2023/7/23
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @PostMapping("/add")
    public ApiBean<?> add(HttpServletRequest request, @RequestBody Article article) {
        long time = DateTimeUtils.getTime();
        String userId = RequestUtils.getUserId(request);
        String userName = RequestUtils.getUserName(request);
        if (StringUtils.isEmpty(article.getTitle())) {
            return ApiBean.failed("标题不能为空");
        }
        if (StringUtils.isEmpty(article.getContent())) {
            return ApiBean.failed("内容不能为空");
        }
        if (StringUtils.isEmpty(article.getTags())) {
            return ApiBean.failed("标签不能为空");
        }
        if (StringUtils.isEmpty(article.getType())) {
            return ApiBean.failed("类型不能为空");
        }
        if (StringUtils.isEmpty(article.getAuthor())) {
            return ApiBean.failed("作者不能为空");
        }
        Article info = ParamsHolder.createNewArticle(article);
        info.setCreator(userId);
        info.setCreatorName(userName);
        info.setCreateTime(time);
        info.setChangeTime(time);
        boolean success = articleService.save(info);
        return ApiBean.success(success);
    }


    @PostMapping("/delete")
    public ApiBean<?> delete(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return ApiBean.failed(false);
        }
        List<Long> hasIds = articleService.getArticleIds(ids);
        int code = articleService.batchDeleteByIds(hasIds);
        return ApiBean.success(code > 0);
    }


    @PostMapping("/edit")
    public ApiBean<?> edit(HttpServletRequest request, @RequestBody Article params) {
        long time = DateTimeUtils.getTime();
        String userId = RequestUtils.getUserId(request);
        String userName = RequestUtils.getUserName(request);
        if (null == params) {
            return ApiBean.failed(false);
        }
        Article into = ParamsHolder.createNewArticle(params);
        into.setChangeTime(time);
        into.setCreator(userId);
        into.setCreatorName(userName);
        boolean success = articleService.update(into, new LambdaQueryWrapper<Article>().eq(Article::getId, params.getId()));
        return ApiBean.success(success);
    }
}
